#include "iic.h"

i2c_master_bus_handle_t bus_handle;
i2c_master_dev_handle_t dev_handle;

esp_err_t iic_init(void)
{
    i2c_master_bus_config_t i2c_bus_config = {
        .clk_source = I2C_CLK_SRC_DEFAULT,
        .i2c_port = I2C_NUM_0,
        .scl_io_num = GPIO_NUM_38,
        .sda_io_num = GPIO_NUM_39,
        .glitch_ignore_cnt = 7,
        .flags.enable_internal_pullup = false,
    };
    ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_config, &bus_handle));

    i2c_device_config_t dev_cfg = {
        .dev_addr_length = I2C_ADDR_BIT_LEN_7,
        .device_address = 0x3c,
        .scl_speed_hz = 100000,
    };
    ESP_ERROR_CHECK(i2c_master_bus_add_device(bus_handle, &dev_cfg, &dev_handle));

    return ESP_OK;
}

esp_err_t i2c_master_wite_bytes(uint8_t *pdata, size_t len)
{
    return i2c_master_transmit(dev_handle, pdata, len, -1);
}